﻿ using Castle.Core.Internal;
using Infrastructure.DataAccess;
using Infrastructure.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Repository
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class,new()
    {
        private readonly DbContext db;
        public BaseRepository(DbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体信息</param>
        public async Task Insert(T entity)
        {
            await db.Set<T>().AddAsync(entity);
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">实体信息集合</param>
        public async Task InsertBatch(List<T> entities)
        {
            await db.Set<T>().AddRangeAsync(entities);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体信息</param>
        public async Task Update(T entity)
        {
            db.Set<T>().Update(entity);
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">实体信息集合</param>
        public async Task UpdateBatch(List<T> entities)
        {
            db.Set<T>().UpdateRange(entities);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体信息</param>
        public async Task Delete(T entity)
        {
            db.Set<T>().Remove(entity);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体信息结合</param>
        public async Task DeleteBatch(List<T> entities)
        {
            db.Set<T>().RemoveRange(entities);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetAll()
        {
            var list = db.Set<T>().AsNoTracking();
            //var list = db.Set<T>().ToList().Where(m => (bool)(m.GetType().GetProperty("IsDeleted").GetValue(m)) == false).AsQueryable().AsNoTracking();
            return list;
        }

    }
}
